#include "popupdialog.h"
#include "qdebug.h"
#include <QPainter>
#include <QApplication>



PopupDialog::PopupDialog(QWidget *parent, bool bAutoCenter, bool bAutoClose):QDialog(parent),m_bAutoCenter(bAutoCenter) {
    if(bAutoClose)
        setWindowFlags(Qt::Popup|Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint);
    else
        setWindowFlags(Qt::Dialog|Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);

    // 创建半透明黑色背景窗口
    QWidget* maskWidget = new QWidget(parent);
    maskWidget->setStyleSheet("background-color: rgba(0, 0, 0, 40%);");
    maskWidget->setGeometry(0, 0, parent->geometry().width(), parent->geometry().height());
    maskWidget->show();
    // 当对话框关闭时删除遮罩层
    connect(this, &QDialog::finished, maskWidget, &QWidget::deleteLater);
    if (bAutoCenter)
        showCenter();
}


void PopupDialog::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);  // 抗锯齿

    // 绘制背景图片
    QPixmap pixmap(":/images/popup_background.png");
    int nRadius = 30;
    int width = this->width();
    int height = this->height();

    // 绘制左上角
    painter.drawPixmap(0, 0, nRadius, nRadius, pixmap, 0, 0, nRadius, nRadius);
    // 绘制右上角
    painter.drawPixmap(width-nRadius, 0, nRadius, nRadius, pixmap, pixmap.width()-nRadius, 0, nRadius, nRadius);
    // 绘制左下角
    painter.drawPixmap(0, height-nRadius, nRadius, nRadius, pixmap, 0, pixmap.height()-nRadius, nRadius, nRadius);
    // 绘制右下角
    painter.drawPixmap(width-nRadius, height-nRadius, nRadius, nRadius, pixmap, pixmap.width()-nRadius, pixmap.height()-nRadius, nRadius, nRadius);

    // 绘制上边
    painter.drawPixmap(nRadius, 0, width-2*nRadius, nRadius, pixmap, nRadius, 0, pixmap.width()-2*nRadius, nRadius);
    // 绘制下边
    painter.drawPixmap(nRadius, height-nRadius, width-2*nRadius, nRadius, pixmap, nRadius, pixmap.height()-nRadius, pixmap.width()-2*nRadius, nRadius);
    // 绘制左边
    painter.drawPixmap(0, nRadius, nRadius, height-2*nRadius, pixmap, 0, nRadius, nRadius, pixmap.height()-2*nRadius);
    // 绘制右边
    painter.drawPixmap(width-nRadius, nRadius, nRadius, height-2*nRadius, pixmap, pixmap.width()-nRadius, nRadius, nRadius, pixmap.height()-2*nRadius);

    // 绘制中间
    painter.drawPixmap(nRadius, nRadius, width-2*nRadius, height-2*nRadius, pixmap, nRadius, nRadius, pixmap.width()-2*nRadius, pixmap.height()-2*nRadius);

    // 绘制文本和其他内容
    QDialog::paintEvent(event);
}

void PopupDialog::showCenter()
{
    QWidget* parentWidget = (QWidget*)parent();
    QRect parentGeometry = parentWidget->geometry();
    QPoint parentCenter = parentGeometry.center();

    // 计算居中位置,使用mapToGlobal确保在正确的屏幕上
    QPoint globalCenter = parentWidget->mapToGlobal(parentCenter - parentGeometry.topLeft());
    int x = globalCenter.x() - (this->width()) / 2;
    int y = globalCenter.y() - (this->height()) / 2;

    // 移动对话框到居中位置
    this->move(x, y);
}

void PopupDialog::resizeEvent(QResizeEvent *event)
{
    Q_UNUSED(event);
    if(m_bAutoCenter)
        showCenter();
}
